<?php
/**
 * --------------------------------------------------------------
 * 手册控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\Manual as ManualModel;
use app\admin\model\ManualTypes as ManualTypesModel;
use app\admin\model\ManualDoc as ManualDocModel;
use util\Tree;

class Manual extends AdminBase
{
    /**
     * 手册列表
     */
    public function index()
    {
        $map  = $this->getSimpleSearchMap();
        $list = ManualModel::where($map['where'])
                ->order('id desc')
                ->paginate(config('app.site_setting.list_rows'), false, [
                    'query' => $map['conditions']
                ]);

        $this->assign([
            'list'         => $list,
            'keyword'      => $map['conditions']['keyword'],
            'search_field' => $map['conditions']['search_field'],
            'title'        => '手册列表'
        ]);
        return $this->fetch();
    }
    
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            
            // 验证
            $result   = $this->validate($data, 'Manual');
            if (true !== $result) $this->error($result);

            if (ManualModel::create($data)) {
                return $this->success('新增成功', url('index'));
            } else {
                return $this->error('新增失败');
            }
		} else {
            $types = ManualTypesModel::queryOneOrList(['status' => 1], 'id,title,pid', 'list', 'id asc');
            $types = $types ? Tree::toFormatTree($types) : [];
            $types = array_merge(array(0 => array('id' => 0, 'title_show' => '顶级分类')), $types);

            $this->assign([
                'types' => $types,
                'title' => '新增手册'
            ]);
		    return $this->fetch();
		}
    }
    
    /**
     * 编辑
     */
    public function edit()
    {
		$id = input('id/d');
    	if ($id <= 0 || !is_numeric($id)) $this->error('参数错误');
	    if ($this->request->isPost()) {
			$data     = $this->request->post();
            
            // 验证
            $result   = $this->validate($data, 'Manual');
            if (true !== $result) $this->error($result);
			
            if (ManualModel::update($data)) {
                return $this->success('编辑成功', url('index'));
            } else {
                return $this->error('编辑失败');
            }
		} else {
            $info  = ManualModel::get($id);
            $types = ManualTypesModel::queryOneOrList(['status' => 1], 'id,title,pid', 'list', 'id asc');
            $types = $types ? Tree::toFormatTree($types) : [];
            $types = array_merge(array(0 => array('id' => 0, 'title_show' => '顶级分类')), $types);

            $this->assign([
                'info'  => $info,
                'types'  => $types,
                'title' => '编辑手册'
            ]);
		    return $this->fetch();
		}
    }
    
    /**
     * 删除
     */
    public function delete($ids = 0)
    {
        (int) $ids || $this->error('参数错误');
        $map = ['id' => $ids];

        $result = ManualModel::where($map)->delete();
        ManualDocModel::where(['manual_id' => $ids])->delete();
        return $result ? $this->success('删除成功!') : $this->error('删除失败!');
    }
    
    /**
	 * 禁用/启用
	 * @param array $ids id数组
     * @param integer $status 状态值
     */
    public function setStatus($ids = [], $state)
    {
        empty($ids) && $this->error('请选择要操作的数据');
        !is_numeric((int) $status) && $this->error('参数错误');
        
        $map  = ['id' => $ids];
        $info = ManualModel::where($map)->setField('status', $status);
        $tips = $status == 1 ? '启用' : '禁用';
        return $info !== FALSE ? $this->success($tips.'成功') : $this->error($tips.'失败');
    }
    
    /**
     * 管理文档
     */
    public function api()
    {
        $manual_id = input('manual_id');
        if ($manual_id <= 0 || !is_numeric($manual_id)) $this->error('参数错误');

        $manual = ManualModel::get($manual_id);
        
        $ManualDocModel = new ManualDocModel();
        $jsonArray      = $ManualDocModel->getProjectTree($manual_id);
        
        $this->assign([
            'title'     => '编辑 '.$manual['title'].' 文档',
            'manual_id' => $manual_id,
            'manual'    => $manual,
            'tree_json' => json_encode($jsonArray)
        ]);

        return $this->fetch();
    }
}